﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Savage.DotNetLibrary.ProtocolLibrary
{
    public class MsmqWrapper
    {
        public MsmqWrapper(string msmqPath)
        {
            _queue = new System.Messaging.MessageQueue(msmqPath);
        }

        private System.Messaging.MessageQueue _queue;

        public void Send(string label, string document)
        {
            Send(label, document, System.Messaging.MessagePriority.Normal);
        }

        public void Send(string label, string document, System.Messaging.MessagePriority priority)
        {
            System.Messaging.Message msg = new System.Messaging.Message(document);
            msg.Recoverable = true;
            msg.Label = label;
            msg.Priority = priority;
            
            Send(msg);
        }

        public void Send(System.Messaging.Message message)
        {
            if (_queue.Transactional)
            {
                System.Messaging.MessageQueueTransaction transaction = new System.Messaging.MessageQueueTransaction();
                transaction.Begin();

                _queue.Send(message, transaction);

                transaction.Commit();
            }
            else
            {
                _queue.Send(message);
            }
        }
    }
}
